"""产品页面"""
from time import sleep
from page.base_page import BasePage
from selenium.webdriver.common.by import By
from page.index_page import IndexPage
from page.login_page import LoginPage

class ProductPage(BasePage):
    url='/crm5/index.php?m=product'
    #添加产品定位器
    add_product_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div:nth-child(2) > div.pull-right > a")
    #添加成功的提示信息定位器
    add_success_locator=(By.XPATH,"/html/body/div[5]/div[2]")
    #产品工具定位器
    source_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div:nth-child(2) > div.pull-right > div > button")
    #导入产品定位器
    import_excel_locator = (By.ID, "import_excel")
    #选择文件定位器
    choose_file_locator = (By.CSS_SELECTOR, "#attachment1 > input[type=file]")
    #导入定位器
    btn_sure_locator=(By.CSS_SELECTOR,"#dialog-import > form > table > tbody > tr:nth-child(4) > td:nth-child(2) > input.btn.btn-primary")
    #取消定位器
    btn_cancel_locator = (By.CSS_SELECTOR, "#dialog-import > form > table > tbody > tr:nth-child(4) > td:nth-child(2) > input:nth-child(2)")
    #产品勾选框定位器
    product_checkbox_locator = (By.NAME, "product_id[]")
    #产品数量显示文本定位器
    product_num_locator=(By.CSS_SELECTOR, "#td_colspan > div.pagination > div:nth-child(1)")
    #产品全部选择框定位器
    product_checkall_locator = (By.ID, "check_all")
    #删除成功定位器
    success_text_locator=(By.CSS_SELECTOR,"#form1 > table > tbody > tr > td")
    #删除按钮定位器
    delete_btn_locator = (By.ID,"delete")

    def click_add_product(self):
        """点击添加产品"""
        self.driver.find_element(*self.add_product_locator).click()
    def click_add_success(self):
        """返回添加成功文本信息"""
        return  self.driver.find_element(*self.add_success_locator).text
    def click_source(self):
        """点击产品工具"""
        self.driver.find_element(*self.source_locator).click()
    def click_import_excel(self):
        """点击导入数据"""
        self.driver.find_element(*self.import_excel_locator).click()
    def click_choose_file(self,file):
        """选择文件"""
        self.driver.find_element(*self.choose_file_locator).send_keys(file)
    def click_btn_sure(self):
        """点击导入"""
        self.driver.find_element(*self.btn_sure_locator).click()
    def click_btn_cancel(self):
        """点击取消"""
        self.driver.find_element(*self.btn_cancel_locator).click()
    def click_product_checkbox(self):
        """产品勾选框"""
        return self.driver.find_elements(*self.product_checkbox_locator)
    def click_delete(self):
        """点击删除"""
        delete_btn = self.driver.find_element(*self.delete_btn_locator)
        # 用 JavaScript 滚动按钮到可视区域（关键：避免被遮挡）
        self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", delete_btn)
        self.driver.execute_script("arguments[0].click();", delete_btn)
    def exist_num(self):
        """获取产品数据信息"""
        return self.driver.find_element(*self.product_num_locator).text
    def click_checkall(self):
        """点击勾选全部产品"""
        self.driver.find_element(*self.product_checkall_locator).click()
    def success_text(self):
        """返回成功文本信息"""
        return self.driver.find_element(*self.success_text_locator).text

    def product_page(self, username, password):
        """ 封装“登录后进入产品页面”的公共流程"""
        lp=LoginPage(self.driver)
        lp.login(username, password)  # 复用登录方法
        ip = IndexPage(self.driver)
        ip.click_product()
        sleep(1)